<?php
/***
* Author:        Dennis T. Kaplan
* Web:           www.pzzazz.com
* Date Created:  01/27/2007
* Last Modified: 01/27/2007
* Version:       1.0
* License:       GPL
* Class			  dbStringIt 
***/

/*************************************************************************
*                                                                       *
* Copyright (c) 2007 Dennis T. Kaplan                                   *
* All rights reserved.                                                  *
*                                                                       *
* This script is free software; you can redistribute it and/or modify   *
* it under the terms of the GNU General Public License as published by  *
* the Free Software Foundation;                                         *
*                                                                       *
* The GNU General Public License can be found at                        *
* http://www.gnu.org/copyleft/gpl.html.                                 *
*                                                                       *
* This script is distributed in the hope that it will be useful,        *
* but WITHOUT ANY WARRANTY; without even the implied warranty of        *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
* GNU General Public License for more details.                          *
*                                                                       *
************************************************************************/
/*

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the finalwebsites.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

_________________________________________________________________________
If you need help check this:
http://www.phpclasses.org/browse/package/3648.html


   To constact the db fields

            Call it like this:
            $upd = new dbStringIt;
            $updatev = $upd->dbUpdate($_POST,$db);

Always first the vaiable which has the new data then the variable with the old data!

      Small modification needed for ezSQL see 2 places below.
*/




class dbStringIt 
{
   function objectArray($getdb){
      foreach ($getdb[0]  as $k => $v) {
         $db[$k] = $v;
      }
      return $db;
   }

   function dbUpdate($vars,$db){
     // $db = $this->objectArray($db); With ecSQL
      if($vars != $db){
         $intersectvar = $this->key_intersect($vars,$db);
            $newValues    = $this->new_value($intersectvar,$db);
         if($newValues > 0){
            $stringIt     = $this->UpdateStrings($newValues);
         }
      }
      if(isset($stringIt)){
         return $stringIt;
      }
   }

      //Returns only the Variable keys which have a corrosponding field in the db.
   function key_intersect($vars,$db) {

      //$db = $this->objectArray($db); //With ezSQL

      // now we compare our stored variables with the table fields from the db
      // and save the once which exist in both in a new variable called intersectvar.
      foreach ($vars as $k => $v) {
         if (array_key_exists($k, $db)){
            $intersectvar[$k] = $v;
         }
      }
      //We return interecting veriables
      if(isset($intersectvar)){
         return $intersectvar;
      }
   }
   // Now we compare the values from the post to the db to find any changed values.
   function new_value($intersectvar,$db) {
      foreach ($intersectvar as $k => $v) {
         if ($db[$k] != $v){
            $newValues[$k] = $v;
         }
      }
      if(isset($newValues)){
         return $newValues;
      }
   }

      // Creates the Update fields for the database
   function UpdateStrings($newValues){
      // We call the function key_intersect note the $this->key_intersect to call a function with in the class
      // If we would call a function from another class we would use funcname::key_intersect
      //$this->updatev = $this->key_intersect($vars,$db);

      // We set this variable so we wont get the error variable doesn't exist
      // when the foreach hits that var the first time.
      $string = '';
      if(is_array($newValues)){
         foreach ($newValues as $k => $v) {
            $string .= $k . " = '". $v ."', " ;
         }
         //here we remove the last comma.
         $string = trim($string);
         $last = $string{strlen($string)-1};
         if (!strcmp($last,",")){
            $string = rtrim($string, ',');
         }
         return $string;
      }
   }
}

?>
